package cn.apthink.calendar;

import cn.apthink.utility.DateUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class TimeRange {
	private int start;
	private int end;
	private boolean overtime;
	
	public TimeRange(int start, int end) {
		this(start, end, false);
	}

	public TimeRange(String start, String end, boolean isOvertime) {
		this(DateUtil.Time2Second(start), DateUtil.Time2Second(end), isOvertime);
	}

	public TimeRange(String start, String end) {
		this(DateUtil.Time2Second(start), DateUtil.Time2Second(end), false);
	}	

	public int duration() {
		int result = 0;
		if (end < start) {
			result = end + (24 * 3600 - start);
		} else {
			result = end - start;
		}
		return result;
	}

	private String _formatHM(int seconds) {
		int h = seconds / 3600;
		int m = (seconds % 3600) / 60;
		return String.format("%02d", h) + ":" + String.format("%02d", m);
	}

	private String _formatHMS(int seconds) {
		int h = seconds / 3600;
		int m = (seconds % 3600) / 60;
		int s = seconds % 60;
		return String.format("%02d", h) + ":" + String.format("%02d", m) + ":" + String.format("%02d", s);
	}

	public String startHMS() {
		return _formatHMS(start);
	}

	public String startHM() {
		return _formatHM(start);

	}

	public String endHMS() {
		return _formatHMS(end);
	}

	public String endHM() {
		return _formatHM(end);
	}
	
	public static TimeRange testData() {
		return new TimeRange("08:00", "12:00");
	}
}
